package com.castle.fortress.admin.oa.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.DayStatisticsEntity;
import com.castle.fortress.admin.oa.dto.DayStatisticsDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.oa.service.DayStatisticsService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 日数据报表 api 控制器
 *
 * @author whc
 * @since 2022-11-04
 */
@Api(tags="日数据报表api管理控制器")
@RestController
@RequestMapping("/api/oa/dayStatistics")
public class ApiDayStatisticsController {
	@Autowired
	private DayStatisticsService dayStatisticsService;


	/**
	 * 日数据报表的分页展示
	 * @param dayStatisticsDto 日数据报表实体类
	 * @param currentPage 当前页
	 * @param size  每页记录数
	 * @return
	 */
	@ApiOperation("日数据报表分页展示")
	@GetMapping("/page")
	public RespBody<IPage<DayStatisticsDto>> pageDayStatistics(DayStatisticsDto dayStatisticsDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
		Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
		Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
		Page<DayStatisticsDto> page = new Page(pageIndex, pageSize);

		IPage<DayStatisticsDto> pages = dayStatisticsService.pageDayStatistics(page, dayStatisticsDto);
		return RespBody.data(pages);
	}

	/**
	 * 日数据报表保存
	 * @param dayStatisticsDto 日数据报表实体类
	 * @return
	 */
	@ApiOperation("日数据报表保存")
	@PostMapping("/save")
	public RespBody<String> saveDayStatistics(@RequestBody DayStatisticsDto dayStatisticsDto){
		if(dayStatisticsDto == null ){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto,DayStatisticsEntity.class);
		if(dayStatisticsService.save(dayStatisticsEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 日数据报表编辑
	 * @param dayStatisticsDto 日数据报表实体类
	 * @return
	 */
	@ApiOperation("日数据报表编辑")
	@PostMapping("/edit")
	public RespBody<String> updateDayStatistics(@RequestBody DayStatisticsDto dayStatisticsDto){
		if(dayStatisticsDto == null || dayStatisticsDto.getId() == null || dayStatisticsDto.getId().equals(0L)){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
			DayStatisticsEntity dayStatisticsEntity = ConvertUtil.transformObj(dayStatisticsDto,DayStatisticsEntity.class);
		if(dayStatisticsService.updateById(dayStatisticsEntity)){
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 日数据报表删除
	 * @param ids 日数据报表id集合
	 * @return
	 */
	@ApiOperation("日数据报表删除")
	@PostMapping("/delete")
	public RespBody<String> deleteDayStatistics(@RequestBody List<Long> ids){
		if(ids == null || ids.isEmpty()){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		if(dayStatisticsService.removeByIds(ids)) {
			return RespBody.data("保存成功");
		}else{
			return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
		}
	}

	/**
	 * 日数据报表详情
	 * @param id 日数据报表id
	 * @return
	 */
	@ApiOperation("日数据报表详情")
	@GetMapping("/info")
	public RespBody<DayStatisticsDto> infoDayStatistics(@RequestParam Long id){
		if(id == null){
			throw new BizException(GlobalRespCode.PARAM_MISSED);
		}
		DayStatisticsDto dayStatisticsDto =  dayStatisticsService.getByIdExtends(id);
		return RespBody.data(dayStatisticsDto);
	}


}
